package com.testingedu.tag;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.testingedu.jdbcDao.TOperateDao;
import com.testingedu.model.TUser;

/**
 * 控制没有权限的按钮不在网页上显示
 * 
 * @author lsww
 * @E-mail lsww.sc@Gmail.com 2012-7-8 下午09:20:57
 * 
 */
public class HasPermissionTag extends BodyTagSupport {

	private static final long serialVersionUID = 1L;
	private String operate;
	private String htmlSrc;
	@Override
	public int doStartTag() throws JspException {
		try {
			StringBuffer results = new StringBuffer();
			HttpSession session = ((HttpServletRequest) pageContext
					.getRequest()).getSession();
			try {
				if (TOperateDao.validatePermission(operate, ((TUser) session
						.getAttribute("testingedu_admin")).getId())) {
					results.append(htmlSrc);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			pageContext.getOut().write(results.toString());
		} catch (IOException ex) {
			throw new JspTagException("验证权限时出现错误");
		}
		return EVAL_BODY_INCLUDE;
	}

	public String getOperate() {
		return operate;
	}

	public void setOperate(String operate) {
		this.operate = operate;
	}

	public String getHtmlSrc() {
		return htmlSrc;
	}

	public void setHtmlSrc(String htmlSrc) {
		this.htmlSrc = htmlSrc;
	}
}
